/**
 * Copyright  (c) 2009-2013 XLab,  Org. 
 * All  rights  reserved. 
 */
package org.xlab.baseframework.dao.impl;

import java.util.Collections;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.xlab.baseframework.dao.api.ISystemCodeDAO;
import org.xlab.baseframework.domain.SystemCode;
import org.xlab.baseframework.exception.ObjectNotFoundException;
import org.xlab.baseframework.exception.TooManyObjectsFoundException;


/**
 * <p>
 * 系统编码数据库访问层接口实现
 * </p>
 * 
 * @author MaKuBEX Email:makubexnet@gmail.com
 * @version 1.0.0 Sep 8, 2010
 * @since 1.0.0
 * 
 */
@Repository
public class SystemCodeDAOImpl extends LogicRemoveDAOImpl<SystemCode, Long>
		implements ISystemCodeDAO {

	/**
	 * @see org.xlab.baseframework.dao.api.ISystemCodeDAO#findAllByCategory(org.xlab.baseframework.domain.SystemCode)
	 */
	public List<SystemCode> findAllByCategory(SystemCode category)
			throws ObjectNotFoundException {
		List<SystemCode> result = null;
		DetachedCriteria criteria = super.generateCriteria();
		criteria.add(Restrictions.eq("category", category));
		result = super.findAllByCriteria(criteria);
		if (result == null || result.isEmpty()) {
			throw new ObjectNotFoundException();
		}
		return result;
	}

	/**
	 * @see org.xlab.baseframework.dao.api.ISystemCodeDAO#findByCode(java.lang.String)
	 */
	public SystemCode findByCode(String code) throws ObjectNotFoundException,
			TooManyObjectsFoundException {
		SystemCode result = super.findByUniqueProperty("code", code);
		return result;
	}

	/**
	 * @see org.xlab.baseframework.dao.api.ISystemCodeDAO#findMaxSequenceByCode(org.xlab.baseframework.domain
	 *      .SystemCode)
	 */
	public Long findMaxSequenceByCode(SystemCode systemCode)
			throws ObjectNotFoundException {
		List<SystemCode> list = super.findAllByExample(systemCode);
		Collections.sort(list);
		return list.get(list.size()).getSequence();
	}
}
